package com.codingke.codingbookstore.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.adapter.OrdersAdapter;
import com.codingke.codingbookstore.utils.Constant;
import com.codingke.codingbookstore.vo.Orders;
import com.codingke.codingbookstore.vo.User;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;

public class OrdersActivity extends BaseActivity implements View.OnClickListener,AdapterView.OnItemClickListener{

    private ListView listView2_orders;
    private OrdersAdapter ordersAdapter;
    private ArrayList<Orders> orderList = new ArrayList<>();
    private ImageView imageView_shopping_cart;

    private int orderStatus;//需要查询的订单状态

    public static final int VIEW_ALL_ORDERS = 0x1;//查看所有订单
    public static final int VIEW_WAIT_PAY_ORDERS = 0x2;//查看待付款订单
    public static final int VIEW_WAIT_GOODS_RECEIPT_ORDERS = 0x3;//查看待收货订单
    public static final int VIEW_WAIT_EVALUATION_ORDERS = 0x4;//查看待评价订单
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orders);
        orderStatus = getIntent().getIntExtra("status",-1);
        if(orderStatus==-1){
            finish();
        }
        imageView_shopping_cart = (ImageView) findViewById(R.id.imageView_shopping_cart);
        imageView_shopping_cart.setOnClickListener(this);
        listView2_orders = (ListView) findViewById(R.id.listView2_orders);
        listView2_orders.setOnItemClickListener(this);
        ordersAdapter = new OrdersAdapter(this,orderList);
        listView2_orders.setAdapter(ordersAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadOrdersData();
    }

    private void loadOrdersData() {
        BmobQuery<Orders> query = new BmobQuery<>();
        query.addWhereEqualTo("userId", BmobUser.getCurrentUser(this, User.class).getObjectId());
        query.order("-buyDate");
        query.include("address");//关联查询对象
        switch (orderStatus){
            case VIEW_ALL_ORDERS:
                query.addWhereGreaterThan("status", Constant.ORDER_NON_PAYMENT);
                break;
            case VIEW_WAIT_PAY_ORDERS:
                query.addWhereEqualTo("status",Constant.ORDER_NON_PAYMENT);
                break;
            case VIEW_WAIT_GOODS_RECEIPT_ORDERS:
                query.addWhereEqualTo("status",Constant.ORDER_DELIVERED);
                break;
            case VIEW_WAIT_EVALUATION_ORDERS:
                query.addWhereEqualTo("status",Constant.ORDER_RECEIVED_GOODS);
                break;
        }

        query.findObjects(this, new FindListener<Orders>() {
            @Override
            public void onSuccess(List<Orders> list) {
                orderList = (ArrayList<Orders>) list;
                ordersAdapter.setOrderList(orderList);
                ordersAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(int i, String s) {
                showToast("加载订单失败");
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView_shopping_cart:
                startActivity(new Intent(this,ShoppingCartActivity.class));
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //待评价
        if(orderStatus==VIEW_WAIT_EVALUATION_ORDERS){
            Orders orders = orderList.get(position);
            Intent intent = new Intent(this,EvaluationActivity.class);
            intent.putExtra("orders",orders);
            startActivity(intent);
            finish();
        }
        //全部订单,待收货
        else if(orderStatus==VIEW_WAIT_GOODS_RECEIPT_ORDERS || orderStatus==VIEW_ALL_ORDERS){
            Orders orders = orderList.get(position);
            Intent intent = new Intent(this,OrderItemActivity.class);
            intent.putExtra("orders",orders);
            startActivity(intent);
            finish();
        }
    }
}
